import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:nft_shop/login/controller/logic_controller.dart';
import 'package:nft_shop/login/controller/user_controller.dart';
import 'package:nft_shop/net/dio_api.dart';
import 'package:nft_shop/net/service_repository.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/widgets/load_image.dart';
import 'package:nft_shop/widgets/my_app_bar.dart';
import 'package:nft_shop/widgets/my_button.dart';
import 'package:nft_shop/widgets/my_scroll_view.dart';
import 'package:nft_shop/widgets/my_text_field.dart';

class MyTradersPasswordPage extends StatefulWidget {
  const MyTradersPasswordPage({
    Key? key,
  }) : super(key: key);

  @override
  _MyTradersPasswordPageState createState() => _MyTradersPasswordPageState();
}

class _MyTradersPasswordPageState extends State<MyTradersPasswordPage> {
  //定义一个controller
  final TextEditingController _passwordController = TextEditingController();
  final TextEditingController _affirmPasswordController =
      TextEditingController();
  final TextEditingController _phoneController = TextEditingController();
  final TextEditingController _vCodeController = TextEditingController();
  final TextEditingController _vImgCodeController = TextEditingController();
  final FocusNode _nodeText1 = FocusNode();
  final FocusNode _nodeText2 = FocusNode();
  final FocusNode _nodeText3 = FocusNode();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        centerTitle: "修改登录密码",
      ),
      body: GetBuilder<UserController>(
        builder: (locgin) {
          return MyScrollView(
            padding: EdgeInsets.symmetric(horizontal: 16),
            children: [
              SizedBox(
                height: 15,
              ),
              Text(
                "新密码",
                style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
              ),
              MyTextField(
                keyName: 'password1',
                focusNode: _nodeText1,
                isInputPwd: true,
                controller: _passwordController,
                keyboardType: TextInputType.visiblePassword,
                hintText: "含字母和数字且长度为6-20位",
              ),
              SizedBox(height: 11),
              Text(
                "确认密码",
                style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
              ),
              MyTextField(
                keyName: 'password2',
                focusNode: _nodeText2,
                isInputPwd: true,
                controller: _affirmPasswordController,
                keyboardType: TextInputType.visiblePassword,
                hintText: "请确认登录密码",
              ),
              SizedBox(height: 11),
              Text(
                "手机号",
                style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
              ),
              AbsorbPointer(
                absorbing: true,
                child: MyTextField(
                  keyName: 'password3',
                  controller: _phoneController,
                  hintText: locgin.member.mobile ?? "",
                ),
              ),
              SizedBox(height: 20),
              MyTextField(
                focusNode: _nodeText3,
                controller: _vCodeController,
                getVCode: () async {
                  EasyLoading.show(status: "加载中...");
                  String url = await ServiceRepository.getImageCode(
                      locgin.member.mobile??"");
                  EasyLoading.dismiss();
                  var imageCode = await showDialog(
                      context: context,
                      builder: (context) {
                        _vImgCodeController.text = "";
                        return Container(
                          alignment: Alignment.center,
                          padding: EdgeInsets.all(30),
                          child: SizedBox(
                            width: 300,
                            height: 330,
                            child: Container(
                              padding: EdgeInsets.all(30),
                              decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(10))),
                              child: Column(
                                children: [
                                  Text(
                                    "请输入验证码",
                                    style: TextStyle(
                                      fontSize: 14,
                                      color: Global.kTheme.textPrimary6,
                                      fontFamily: Global.mySemiboldFont,
                                    ),
                                  ),
                                  SizedBox(
                                    height: 10,
                                  ),
                                  LoadImage(
                                    url,
                                    fit: BoxFit.fitWidth,
                                  ),
                                  SizedBox(
                                    height: 10,
                                  ),
                                  Container(
                                    padding: EdgeInsets.only(left: 5, right: 5),
                                    decoration: BoxDecoration(
                                      color: Color(0xffF4F4F4),
                                      borderRadius:
                                          BorderRadius.all(Radius.circular(5)),
                                    ),
                                    child: MyTextField(
                                      keyboardType: TextInputType.phone,
                                      hintText: "请输入验证码",
                                      controller: _vImgCodeController,
                                    ),
                                  ),
                                  SizedBox(
                                    height: 10,
                                  ),
                                  MyButton(
                                      minHeight: 40,
                                      text: "确认",
                                      fontSize: 17,
                                      textColor: Global.kTheme.textPrimary5,
                                      backgroundColor:
                                          Global.kTheme.colorPrimary,
                                      onPressed: () {
                                        if (_vImgCodeController.text.length ==
                                            0) {
                                          EasyLoading.showToast('请输入验证码');
                                          return;
                                        }
                                        Navigator.of(context)
                                            .pop(_vImgCodeController.text);
                                      })
                                ],
                              ),
                            ),
                          ),
                        );
                      });
                  if (ObjectUtil.isNotEmpty(imageCode)) {
                    return LoginLogic.to
                        .shayuSms(locgin.member.mobile ?? "", imageCode);
                  } else {
                    return false;
                  }
                },
                maxLength: 6,
                keyboardType: TextInputType.phone,
                hintText: "请输入验证码",
              ),
              SizedBox(height: 51),
              Container(
                  height: 40,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    // Colours.app_main,
                    borderRadius: BorderRadius.circular(5),
                  ),
                  child: MyButton(
                    key: const Key('register'),
                    minHeight: 40,
                    onPressed: () async {
                      _nodeText1.unfocus(); // 失去焦点
                      _nodeText2.unfocus(); // 失去焦点
                      _nodeText3.unfocus(); // 失去焦点

                      if (_passwordController.text.length == 0) {
                        EasyLoading.showToast('请输入密码');
                        return;
                      }
                      if (_passwordController.text.length < 6 ||
                          _passwordController.text.length > 20) {
                        EasyLoading.showToast('密码含字母和数字且长度为6-20位');
                        return;
                      }

                      if (_affirmPasswordController.text.length == 0) {
                        EasyLoading.showToast('请输入确认密码');
                        return;
                      }

                      if (_passwordController.text !=
                          _affirmPasswordController.text) {
                        EasyLoading.showToast('两次密码不一致');
                        return;
                      }

                      if (_vCodeController.text.length == 0) {
                        EasyLoading.showToast('请输入验证码');
                        return;
                      }
                      EasyLoading.show(status: '加载中...');
                      try {
                        await ServiceRepository.setSafetyCode(
                            mobile: locgin.member.mobile!,
                            safety_code: _passwordController.text,
                            resafecode: _affirmPasswordController.text,
                            verify_code: _vCodeController.text);
                        EasyLoading.showToast("修改成功");
                        Get.back();
                      } catch (e, s) {
                        EasyLoading.showToast(http.setError(e, s));
                      }
                    },
                    text: "确定",
                    fontSize: 16,
                    textColor: Colors.white,
                    backgroundColor: Global.kTheme.colorBackground3,
                  )),
            ],
          );
        },
      ),
    );
  }
}

enum ModifyType { trading, login }
